package csu.web.mypetstore.servlet;

import csu.web.mypetstore.domain.Item;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Xi_Chen
 * @date 2023/12/10
 * @Description
 */
public class ItemForm extends HttpServlet {
    private CatalogService catalogService;
    private static final String ITEM_FORM = "/WEB-INF/jsp/catalog/item.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String itemId = req.getParameter("itemId");
        catalogService = new CatalogService();
        Item item = catalogService.getItem(itemId);
        String str = item.getProduct().getDescription();
        String description = str.substring(0, str.indexOf('>')+1);
        String img = str.substring(str.indexOf('>')+1, str.length()-1);

        HttpSession httpSession = req.getSession();
        httpSession.setAttribute("item", item);
        httpSession.setAttribute("description", description);
        httpSession.setAttribute("img", img);
        req.getRequestDispatcher(ITEM_FORM).forward(req, resp);
    }
}
